package com.ibm.workplace.wcm.cqplugin;

import java.util.*;

import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.jobs.*;

import com.ibm.workplace.wcm.cqplugin.cq.*;
import com.ibm.workplace.wcm.cqplugin.cqweb.CQHttpTest;
import com.ibm.workplace.wcm.cqplugin.ui.*;

public class PerformBugSearchJob extends Job {

    private IBugQuery query;
    private IProgressPresenter pp;

    private IBugInfo[] results;
    
    public PerformBugSearchJob(IBugQuery query) {
        this(query, null);
    }

    public PerformBugSearchJob(IBugQuery query, IProgressPresenter progressPresenter) {
        super("Defect Search");

        this.query = query;
        this.pp = progressPresenter;
    }

    protected IStatus run(IProgressMonitor monitor) {
		
    	//comment out to avoid error
    	/*
    	String[] profileNames = query.getConnectionProfileNames();
        IConnectionProfile[] connectionProfiles = null;
        
        if (profileNames == null) {
            connectionProfiles = BugInfoPluginActivator.getDefault().getConnectionProfiles();
        } else {
            java.util.List tempProfiles = new ArrayList();
            
            for (int i = 0; i < profileNames.length; i++) {
                IConnectionProfile profile = BugInfoPluginActivator.getDefault().getConnectionProfile(profileNames[i]);
                if (profile != null) {
                    tempProfiles.add(profile);
                }
            }
            
            connectionProfiles = (IConnectionProfile[])tempProfiles.toArray(new IConnectionProfile[tempProfiles.size()]);
        }
        
        if (connectionProfiles != null && connectionProfiles.length > 0) {
        */
            java.util.List fullResults = new ArrayList();
         
            IBugSearchCriteria bsc = query.getSearchCriteria();
            // iterate through connections and perform search
            /*for (int i = 0; i < connectionProfiles.length && !monitor.isCanceled(); i++) {*/
                CqDefectSearchEngine cdse = new CqDefectSearchEngine();                
                try {
                    IBugInfo[] results = cdse.performSearch(bsc, monitor, pp);
                    if (results != null) {
                        for (int j = 0; j < results.length; j++) {
                            fullResults.add(results[j]);
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            //}
            
            // set the results on the job so the caller can get to them
            this.results = (IBugInfo[])fullResults.toArray(new IBugInfo[fullResults.size()]);
            
        /*
        } else {
            this.results = new IBugInfo[0];
        }*/
        
        if (monitor.isCanceled()) {
            return Status.CANCEL_STATUS;
        } else {
            return Status.OK_STATUS;
        }
     

    }

    public IBugInfo[] getResults() {
        return results;
    }

}
